home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / include / BT_Tuple.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  5.3 KB  |  227 lines

  1. #ifndef BT_TUPLE_H
  2. #define BT_TUPLE_H
  3. /*
  4.  *    $RCSfile: BT_Tuple.h,v $
  5.  *    $Revision: 1.1.1.1 $
  6.  *    $Date: 1996/05/04 21:55:07 $
  7.  */
  8. /**********************************************************************
  9.  * EXODUS Database Toolkit Software
  10.  * Copyright (c) 1991 Computer Sciences Department, University of
  11.  *                    Wisconsin -- Madison
  12.  * All Rights Reserved.
  13.  *
  14.  * Permission to use, copy, modify and distribute this software and its
  15.  * documentation is hereby granted, provided that both the copyright
  16.  * notice and this permission notice appear in all copies of the
  17.  * software, derivative works or modified versions, and any portions
  18.  * thereof, and that both notices appear in supporting documentation.
  19.  *
  20.  * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  21.  * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  22.  * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  23.  * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  24.  *
  25.  * The EXODUS Project Group requests users of this software to return 
  26.  * any improvements or extensions that they make to:
  27.  *
  28.  *   EXODUS Project Group 
  29.  *     c/o David J. DeWitt and Michael J. Carey
  30.  *   Computer Sciences Department
  31.  *   University of Wisconsin -- Madison
  32.  *   Madison, WI 53706
  33.  *
  34.  *     or exodus@cs.wisc.edu
  35.  *
  36.  * In addition, the EXODUS Project Group requests that users grant the 
  37.  * Computer Sciences Department rights to redistribute these changes.
  38.  **********************************************************************/
  39.  
  40. /*
  41.  *    A tuple could be of type LEAF or NODE. LEAF tuple contains elCnt 
  42.  *    and ovFlag. NODE tuple contains childPid.
  43.  */
  44. enum BT_TupleType { LEAFTUPLE = 0x37, NODETUPLE };
  45.  
  46. struct LeafAttr {
  47.     ONE    ovFlag;        // overflow flag
  48.     ONE    elSize;        // element size
  49.     TWO    elCnt;        // oid counter
  50. };
  51.  
  52. struct NodeAttr {
  53.     SHORTPID    childPid;
  54. };
  55.  
  56. union Attr_U {
  57.     LeafAttr    leafAttr;
  58.     NodeAttr    nodeAttr;
  59. };
  60.  
  61. /*
  62.  *    Size declaration of a tuple. 
  63.  */
  64. const MAXTUPLESIZE = BT_DATASIZE / 2 - sizeof(SLOT);
  65. const FIXEDMEM       = sizeof(TWO) + 2 * sizeof(ONE) + sizeof(Attr_U);
  66. const MAXKEYLEN       = MAXTUPLESIZE - FIXEDMEM - SM_MAXELEMLEN;
  67.  
  68.  
  69.  
  70. /*
  71.  *     BT_Tuple class. 
  72.  */
  73. class BT_Tuple {
  74.     
  75.     TWO        klen;        // length of key 
  76.     ONE        align;        // alignment added to key
  77.     ONE        type;        // BT_TupleTupe
  78.     Attr_U    attr;        // attribute of the tuple 
  79.     union {    
  80.     char    key[1];        // key and (oidArray or overflowRoot)
  81.     ONE    total_space[MAXTUPLESIZE - FIXEDMEM];
  82.     };
  83.     
  84. public:
  85.     
  86.     /*
  87.      *    Functions to manipulate OidArray
  88.      */
  89.     void*    ElArray(int idx);
  90.     int        AddElem(const void* newElem);
  91.     int        RemoveElem(const void* doomedElem);
  92.     
  93.     //
  94.     //    Functions to manipulate oid counter
  95.     //
  96.     int        ElCnt();
  97.     int        ElSize();
  98.     void    IncrElCnt();
  99.     void     DecrElCnt();
  100.     
  101.     //
  102.     //    Functions to get key attributes
  103.     //
  104.     int     KeyLen()        { return klen; }
  105.     void*     KeyValue()        { return key; }
  106.     
  107.     //
  108.     //    Get child pointer
  109.     //
  110.     SHORTPID Vector()        { 
  111.     ASSERT3(IsNodeTuple());
  112.     return attr.nodeAttr.childPid; 
  113.     }
  114.     
  115.     //
  116.     //    Functions dealing with tuple overflow
  117.     //
  118.     int        WillOverflow()    { 
  119.     ASSERT3(IsLeafTuple());
  120.     return Size() + ElSize() > MAXTUPLESIZE; 
  121.     }
  122.     int        IsOverflow()    { 
  123.     ASSERT3(IsLeafTuple());
  124.     return attr.leafAttr.ovFlag; 
  125.     }
  126.     void    SetOverflow(const PID& ovRoot);
  127.     void    ResetOverflow(int cnt, const void* elList);
  128.     PID&    OverflowRoot();
  129.     
  130.     //
  131.     //    Checks
  132.     //
  133.     int     IsNodeTuple()    { return type == NODETUPLE; }
  134.     int     IsLeafTuple()    { return type == LEAFTUPLE; }
  135.     
  136.     int        PredictSize(int keyLen)  {
  137.     ASSERT3(this == 0);
  138.     return ALIGNSIZE(keyLen) + (key-(ONE*)this) + SM_MAXELEMLEN;
  139.     }
  140.     int        Size();
  141.  
  142.     /* create leaf */
  143.     void     Create(int keyLen, const void* keyPtr,
  144.                const void* elem, int elSize);
  145.  
  146.     /* create node */
  147.     void     Create(int keyLen, void* keyPtr, SHORTPID child);
  148.  
  149. };
  150.  
  151.  
  152. #if (defined DEBUG && defined btree_MODULE) || ! defined DEBUG
  153.  
  154. INLINE int BT_Tuple::ElSize()
  155.     ASSERT3(IsLeafTuple());
  156.     ASSERT3(attr.leafAttr.elSize >=0 &&
  157.         attr.leafAttr.elSize <= SM_MAXELEMLEN);
  158.     return attr.leafAttr.elSize;
  159. }
  160.  
  161. INLINE void* BT_Tuple::ElArray(int idx)
  162. {
  163.     ASSERT3(!IsOverflow());
  164.     ASSERT3(idx < attr.leafAttr.elCnt);
  165.     return key + (klen + align + idx * ElSize());
  166. }
  167.  
  168. INLINE int BT_Tuple::ElCnt()
  169.     ASSERT3(IsLeafTuple());
  170.     return attr.leafAttr.elCnt; 
  171. }
  172.  
  173.  
  174. INLINE void BT_Tuple::IncrElCnt()
  175.     ASSERT3(IsOverflow());
  176.     attr.leafAttr.elCnt++;
  177. }
  178.  
  179.  
  180. INLINE void BT_Tuple::DecrElCnt()
  181.     ASSERT3(IsOverflow());
  182.     ASSERT3(ElCnt() > 0);
  183.     attr.leafAttr.elCnt--;
  184. }
  185.  
  186. INLINE BT_Tuple::Size()
  187. {
  188.     int len = align + klen + (key - (ONE*)this);
  189.     if (type == LEAFTUPLE)
  190.     len += ALIGNSIZE(IsOverflow() ? sizeof(PID) : ElCnt() *
  191.              ElSize());
  192.  
  193.     return len;
  194. }
  195.  
  196.  
  197. INLINE void BT_Tuple::SetOverflow(const PID& ovRoot)
  198.     ASSERT3(IsLeafTuple());
  199.     ASSERT3(attr.leafAttr.ovFlag == 0);
  200.     attr.leafAttr.ovFlag = 1;
  201.     OverflowRoot() = ovRoot;
  202. }
  203.  
  204. INLINE void BT_Tuple::ResetOverflow(int cnt, const void* elList) 
  205. {
  206.     ASSERT3(IsLeafTuple());
  207.     ASSERT3(IsOverflow());
  208.     ASSERT3(cnt == ElCnt());
  209.     attr.leafAttr.ovFlag = 0;
  210.     BCOPY(elList, ElArray(0), cnt * ElSize());
  211. }
  212.  
  213.  
  214. INLINE PID& BT_Tuple::OverflowRoot()
  215.     ASSERT3(IsOverflow());
  216.     return *((PID*)(key+klen+align));
  217. }
  218.  
  219. #endif /* (DEBUG && btree_MODULE) || !DEBUG */
  220. #endif    // BT_TUPLE_H
  221.